// subpage/meter-search/meter-search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 2,
    pagination: {
      page: 1,
      rows: 5,
      sidx: '',
      sord: ''
    },
    count: 0,
    dataList: [],
    hasNextPage: true,
    noData: false,
    isReachBottom: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDataList(true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.getDataList(true);
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      isReachBottom: true
    });
    if (this.data.hasNextPage) {
      this.setData({
        'pagination.page': this.data.pagination.page + 1
      })
      this.getDataList();
    } 
    // else {
    //   wx.showToast({
    //     title: '没有更多数据了',
    //     icon: 'none',
    //     duration: 1000
    //   });
    // }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  _refreshData: function () {
    this.setData({ //初始化数据
      "pagination.page": 1,
      count: 0,
      dataList: [],
      hasNextPage: true,
      noData: false,
      isReachBottom: false,
    })
  },
  tabClick(e) {
    this._refreshData();
    this.setData({
      activeIndex: parseInt(e.currentTarget.dataset.index),
    })
    this.getDataList(true);
  },
  getDataList1(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let params = {
      pagination: this.data.pagination,
      queryParam: {
        WxStatus: "3",
        IsFinish: false,
        NeedAudit: 0
      }
    };
    http.GetOrderList(params).then(res => {
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.Items]
        } else {
          dataList = [...this.data.orderList, ...res.data.Items]
        }
        let hasNextPage = res.data.HasNextPages
        const noData = dataList.length === 0
        this.setData({
          orderList: dataList,
          count: res.data.TotalCount,
          hasNextPage: hasNextPage,
          noData: noData
        })
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  getDataList(isRefresh = false) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    setTimeout(() => {
      const res = [];
      for(let i = 0; i < this.data.pagination.rows; i++){
        res.push({
          CONS_NAME: '孙晶晶',
          CONS_NO: '126930',
          HEAT_AREA: parseFloat(Math.random() * 20 + 30).toFixed(2),
          METER_NO: '123490',
          LAST_NUM: parseFloat(Math.random() * 50 + 50).toFixed(1),
          THIS_NUM: this.data.activeIndex === 1 ? parseFloat(Math.random() * 50 + 50).toFixed(1) : null,
          HEAT_TYPE: i,
          Status: this.data.activeIndex === 1 ? 1 : 0,
          OPER: '刘新',
          date: '2025-08-21 18:32:35'
        })
      }
      let dataList = [];
      if (isRefresh) {
        dataList = [...res]
      } else {
        dataList = [...this.data.dataList, ...res]
      }
      let hasNextPage = dataList.length < 30;
      const noData = dataList.length === 0;
      this.setData({
        dataList: dataList,
        hasNextPage: hasNextPage,
        noData: noData
      })
      wx.hideLoading();
    }, 2000)
  },
  toControl(e){
    const meterNo = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../meter-control/meter-control?meterno=" + meterNo
    })
  }
})